<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetFiles;

use App\Http\Center\Common\ErrorCodeService;
use App\Http\Center\Common\LogService;
use App\Http\Center\Common\ValidateService;
use App\Http\Center\Helper\PluginHelper;
use App\Http\Center\Helper\CmdRpcHelper;
use App\Http\Center\Scene\FileSceneService;
use App\Http\FresnsApi\Helpers\ApiConfigHelper;
use App\Http\FresnsCmd\FresnsCmdWords;
use App\Http\FresnsCmd\FresnsCmdWordsConfig;
use App\Plugins\FcpApi\Base\TweetBaseController;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigs;
use Illuminate\Http\Request;

class FsControllerAdmin extends TweetBaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->service = new FsService();
    }

    //上传图片
    public function upload(Request $request)
    {
        $t1 = time();

        // 获取UploadFile的实例
        $uploadFile = $request->file('file');

        $unikey = ApiConfigHelper::getConfigByItemKey('images_service');
        $pluginUniKey = $unikey;
        // 执行上传
        $pluginClass = PluginHelper::findPluginClass($pluginUniKey);

        if (empty($pluginClass)) {
            LogService::error('未找到插件类');
            $this->error(ErrorCodeService::PLUGINS_CLASS_ERROR);
        }

        $isPlugin = PluginHelper::pluginCanUse($pluginUniKey);
        if ($isPlugin == false) {
            LogService::error('未找到插件类');
            $this->error(ErrorCodeService::PLUGINS_CLASS_ERROR);
        }

        $file['file_type'] = $request->input('type', 1);
        $paramsExist = false;

        $configMapInDB = TweetConfigs::whereIn('item_key', ['images_secret_id', 'images_secret_key', 'images_bucket_domain'])->pluck('item_value',
            'item_key')->toArray();
        $paramsExist = ValidateService::validParamExist($configMapInDB,
            ['images_secret_id', 'images_secret_key', 'images_bucket_domain']);

        if ($paramsExist == false) {
            LogService::error('插件信息未配置');
            $this->error(ErrorCodeService::PLUGINS_PARAM_ERROR);
        }

        // 确认目录
        $options['file_type'] = $request->input('type', 1);
        $options['table_type'] = $request->input('tableType');
        $storePath = FileSceneService::getEditorPath($options);

        if (! $storePath) {
            $this->error(ErrorCodeService::CODE_PARAM_ERROR);
        }

        // 获取UploadFile的实例
        $uploadFile = $request->file('file');

        if (empty($uploadFile)) {
            $this->error(ErrorCodeService::UPLOAD_FILES_SUFFIX_ERROR);
        }

        $cmd = FresnsCmdWordsConfig::FRESNS_CMD_UPLOAD_FILE;
        $input = [];
        $input['type'] = $request->input('file_type', 1);
        $input['tableType'] = $request->input('table_type', 2);
        $input['tableName'] = $request->input('table_name', 'configs');
        $input['tableField'] = $request->input('table_field', 'item_value');
        $input['tableId'] = $request->input('table_id', null);
        $input['tableKey'] = $request->input('table_key', null);
        $input['mode'] = 1;
        $input['file'] = $request->file('file');
        $input['fileInfo'] = null;
        $input['platform'] = $request->header('platform', 1);
        $resp = CmdRpcHelper::call(FresnsCmdWords::class, $cmd, $input);
        if (CmdRpcHelper::isErrorCmdResp($resp)) {
            $this->errorCheckInfo($resp);
        }

        $output = $resp['output'];

        $fid = $output['files'][0]['fid'] ?? 0;

        $files = TweetFiles::where('uuid', $fid)->first();

        $data = [];

        $imagesHost = ApiConfigHelper::getConfigByItemKey('images_bucket_domain');
        $imagesConfig = ApiConfigHelper::getConfigByItemKey('images_thumb_config');

        $data['file_id'] = $files['id'];
        $data['file_url'] = $imagesHost.$files['file_path'].$imagesConfig;

        $this->success($data);
    }

    // 验证规则
    public function rules($ruleType)
    {
        $rule = [];

        $config = new FsConfig($this->service->getTable());

        switch ($ruleType) {
            case FsConfig::RULE_STORE:
                $rule = $config->storeRule();
                break;

            case FsConfig::RULE_UPDATE:
                $rule = $config->updateRule();
                break;

            case FsConfig::RULE_DESTROY:
                $rule = $config->destroyRule();
                break;

            case FsConfig::RULE_DETAIL:
                $rule = $config->detailRule();
                break;
        }

        return $rule;
    }
}
